package avicit.bdp.dcs.utils.api.response;

import avicit.bdp.dcs.utils.api.Configuration;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;

public class JsonAdapter extends ResponseAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(JsonAdapter.class);

    private static JsonAdapter instance;

    private JsonAdapter() {
    }

    public synchronized static JsonAdapter instance() {
        if (instance == null) {
            instance = new JsonAdapter();
        }
        return instance;
    }

    /**
     * 解析响应数据
     * @param readerSliceConfig
     * @param recordSender
     * @param result
     * @param columnList
     */
    public String parseResponseData(Configuration readerSliceConfig, ResponseEntity<String> result){
        LOG.info("parse api response data...");

        // 解析状态码
        parseStatusCode(readerSliceConfig, result);

        String body = result.getBody();
//        String text = JSONObject.toJSONString(body);

        try{
//          String str = JSONObject.toJSONString(s, WriteMapNullValue, WriteNullStringAsEmpty, WriteNullListAsEmpty, MapSortField);
            body = body.replaceAll("null","\"\"");
        }catch (Exception e){
            e.printStackTrace();
        }

        return body;
    }

}
